package com.wjf.accesscontrol.net.interceptor

import android.os.Build
import com.wjf.accesscontrol.util.AppUtils
import com.wjf.accesscontrol.util.CONTROL_IDENTIFY
import com.wjf.accesscontrol.util.Constant
import com.wjf.accesscontrol.util.LogUtils
import okhttp3.Interceptor
import okhttp3.Response

/**
 * @Description
 * @Author WuJianFeng
 * @Date 2024/1/31 13:51
 *
 */

class HeadInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        val original = chain.request()
        val requestBuilder = original.newBuilder()
            .addHeader("model", Build.MANUFACTURER + " " + Build.MODEL)
            .addHeader("localsizeModel", "Android")
            .addHeader("systemName", "Android " + Build.VERSION.RELEASE)
            .addHeader("systemVersion", "${Build.VERSION.SDK_INT}")
            .addHeader("Authorization", "")
            .addHeader("mobileName", Build.MANUFACTURER)
            .addHeader("appVersion", "1.0") // Application.getAppVersionName()
            .addHeader("identity", CONTROL_IDENTIFY)
            .addHeader("Content-Type", "application/json; charset=UTF-8")

        if (Constant.serviceId != "" && Constant.serviceMethod != ""){
            requestBuilder
                .addHeader("serviceId", Constant.serviceId)
                .addHeader("serviceMethod", Constant.serviceMethod)
        }


        LogUtils.logAndWrite("__https-请求接口","${Constant.serviceId}/${Constant.serviceMethod}")

        AppUtils.getToken() { token, timestamp ->
            requestBuilder
                .addHeader("token", token)
                .addHeader("timestamp", timestamp)

        }
        return chain.proceed(requestBuilder.build())
    }
}